package com.atguigu.yygh.host.api.API;

import com.atguigu.yygh.common.Result;
import com.atguigu.yygh.host.service.ScheduleService;
import com.atguigu.yygh.host.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author DongYu Zhou
 * @date 2022/6/24 21:51
 */
@Api(tags = "保存排班信息")
@RestController
@RequestMapping("/api/hosp")
@SuppressWarnings("all") //压制所有警告 很烦
public class APIScheduleController {

    @Autowired
    private ScheduleService scheduleService;


    @ApiOperation(value = "保存排班信息")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //1获取并转化参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //2签名校验 省略
        //3调用接口保存数据
        scheduleService.save(paramMap);
        //4返回结果
        return Result.ok();
    }

    @ApiOperation(value = "查询分页科室信息")
    @PostMapping("/schedule/list")
    public Result departmentList(HttpServletRequest request) {

        // 1.获取数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);
        // 签名校验省略
        String hoscode = (String) stringObjectMap.get("hoscode");
        String sign = (String) stringObjectMap.get("sign");

        // page 和 limit

        int page = StringUtils.isEmpty(stringObjectMap.get("page")) ? 1 : Integer.parseInt((String) stringObjectMap.get("page"));
        int limit = StringUtils.isEmpty(stringObjectMap.get("limit")) ? 10 : Integer.parseInt((String) stringObjectMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);

        //查询
        Page<Schedule> pageModel = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室信息")
    @PostMapping("schedule/remove")
    public Result departmentRemove(HttpServletRequest request) {
        // 1.获取数据
        String hosScheduleId = request.getParameter("hosScheduleId");

        scheduleService.remove(hosScheduleId);

        return Result.ok();
    }

}
